﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Web.Security;
using System.Web.UI.WebControls;
using SalesAndStoreApp.BLL;
using SalesAndStoreApp.DAL.DAO;

namespace SalesAndStoreApp.UI.StoreManager
{
    public partial class SaveProductInStore : System.Web.UI.Page
    {
        private ProductManager productManager = new ProductManager();
        private VendorManager vendorManager = new VendorManager();
        private double totalPrice;
        private PurchaseManager purchaseManager = new PurchaseManager();
        private InventoryManager inventoryManager = new InventoryManager();

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetNoStore();
            if (!IsPostBack)
            {
                try
                {
                    messageLabel.Text = "";
                    productDropDownList.DataSource = productManager.GetAll();
                    productDropDownList.DataTextField = "Name";
                    productDropDownList.DataValueField = "Id";
                    productDropDownList.DataBind();
                    vendorDropDown.DataSource = vendorManager.GetAll();
                    vendorDropDown.DataTextField = "Name";
                    vendorDropDown.DataValueField = "Id";
                    vendorDropDown.DataBind();
                    if (productDropDownList.Items.Count != 0)
                    {
                        Product product = productManager.GetById(int.Parse(productDropDownList.Items[0].Value));
                        unitPriceLabel.Text = product.BuyUnitPrice.ToString();
                    }
                    Session["products"] = new List<Product>();
                    dateTextBox.Text = DateTime.Now.Date.ToShortDateString();
                    int defaultQuantity = 1;
                    quantityTextBox.Text = defaultQuantity.ToString();
                    totalPriceLabel.Text = new int().ToString();
                }
                catch (Exception exception)
                {
                    Response.Redirect("~/UI/ErrorPage.aspx?Error=" + exception.Message);
                }
                
            }
            

        }

        protected void addButton_Click(object sender, EventArgs e)
        {
            try
            {
                List<Product> products = GetProductsFromProductView();
                Product product = productManager.GetById(int.Parse(productDropDownList.SelectedValue));
                product.Inventory.Quantity = Convert.ToInt32(quantityTextBox.Text);
                Session["products"] = productManager.AddProductTo(product, products);
                productStoreListView.DataBind();
                foreach (var product1 in products)
                {
                    totalPrice += product1.BuyUnitPrice * product1.Inventory.Quantity;
                }
                totalPriceLabel.Text = totalPrice.ToString(CultureInfo.InvariantCulture);
            }
            catch (Exception exception)
            {
                Response.Redirect("~/UI/ErrorPage.aspx?Error= Error occured while adding products in specific- "+exception.Message);
            }
            
           
        }

        

        private List<Product> GetProductsFromProductView()
        {
            try
            {
                var products = new List<Product>();
                foreach (var item in productStoreListView.Items)
                {
                    Label productIdLabel = (Label)item.FindControl("IdLabel");
                    Label productNameLabel = (Label)item.FindControl("NameLabel");
                    Label productUnitPriceLabel = (Label)item.FindControl("UnitPriceLabel");
                    Label productQuantity = (Label)item.FindControl("QuantityLabel");
                    Product product = new Product();
                    product.Id = int.Parse(productIdLabel.Text);
                    product.Name = productNameLabel.Text;
                    product.BuyUnitPrice = Convert.ToDouble(productUnitPriceLabel.Text);
                    product.Inventory = inventoryManager.GetInventoryOfProduct(product.Id);
                    product.Inventory.Quantity = Convert.ToInt32(productQuantity.Text);
                    products.Add(product);
                }
                return products;
            }
            catch (Exception exception)
            {
                throw new Exception("Error occured while trying to sweep the list of previous product from product queue list",exception);
            }
           
        }

        protected void productDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Product product = productManager.GetById(int.Parse(productDropDownList.SelectedValue));
                unitPriceLabel.Text = product.BuyUnitPrice.ToString();
            }
            catch (Exception exception)
            {
                Response.Redirect("~/UI/ErrorPage.aspx?Error=" + exception.Message);

            }
            
        }

        protected void logOutLink_Click(object sender, EventArgs e)
        {
            try
            {
                FormsAuthentication.SignOut();
                Response.Cache.SetNoStore();
                FormsAuthentication.RedirectToLoginPage();
            }
            catch (Exception exception)
            {
                Response.Redirect("~/UI/ErrorPage.aspx?Error=" + exception.Message);

            }
            
        }

        protected void saveButton_Click(object sender, EventArgs e)
        {
            try
            {
                ProductCarriage productCarriage = new ProductCarriage();
                productCarriage.Products = GetProductsFromProductView();
                productCarriage.Date = Convert.ToDateTime(dateTextBox.Text).Date;
                productCarriage.TotalPrice = Convert.ToDouble(totalPriceLabel.Text);
                productCarriage.Vendor = vendorManager.GetById(int.Parse(vendorDropDown.SelectedValue));
                if (purchaseManager.Save(productCarriage))
                {
                    messageLabel.ForeColor = Color.Green;
                    messageLabel.Text = "Saved Successfully!";
                }
            }
            catch (Exception exception)
            {
                
                messageLabel.ForeColor = Color.Red;
                messageLabel.Text = exception.Message;
            }
            
            
        }
    }

    
}